package com.facemox.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.facemox.bean.EnvConfig;
import com.facemox.bean.FaceInfo;
import com.facemox.bean.Module;
import com.facemox.bean.PermissionAuthentication;
import com.facemox.bean.Project;
import com.facemox.bean.ServerMonitor;
import com.facemox.service.EnvConfigService;
import com.facemox.service.FaceInfoService;
import com.facemox.service.ModuleService;
import com.facemox.service.StatisticService;

@Controller
public class ModuleController {
    
    @Autowired
    private ModuleService moduleService;
    
    @Autowired
    private StatisticService statisticService;
    
    @Autowired
    private FaceInfoService faceInfoService;
    
    @Autowired
    private EnvConfigService envConfigService;
    
    @Autowired
    private Module module;
    
    @Autowired
    private Project project;
    
    @RequestMapping("/")
    public String getProjectList(ModelMap modelMap) {
        List<Project> projectList = moduleService.getAllProject();
        List<Module> moduleList = moduleService.getAllModule();
        List<PermissionAuthentication> authenticationList = moduleService.getAllAuthorization();
        List<ServerMonitor> serverMonitorList = statisticService.getServerMonitor();
        int serverMonitorCount = statisticService.getServerMonitorCount();
        List<FaceInfo> faceList = faceInfoService.getFaceAll();
        EnvConfig config = envConfigService.getEnvConfig();
        String paramBody = "";
        Map<String, String> mapStr = new HashMap<String, String>();
        if (config != null) {
            paramBody = config.getParamBody();

            // List<String> envConfiglist = new ArrayList<String>();
            Map<String, Object> map = new HashMap<String, Object>();
            if (paramBody != null) {
                // envConfiglist = JSONObject.parseArray(paramBody, String.class);
                map = JSON.parseObject(paramBody);
            }

            for (String key : map.keySet()) {
                String value = map.get(key).toString();
                mapStr.put(key, map.get(key).toString());
            }
        }
        modelMap.addAttribute("projectList", projectList);
        modelMap.addAttribute("moduleList", moduleList);
        modelMap.addAttribute("authenticationList", authenticationList);
        modelMap.addAttribute("serverMonitorList", serverMonitorList);
        modelMap.addAttribute("serverMonitorCount", serverMonitorCount);
        modelMap.addAttribute("faceList", faceList);
        modelMap.addAttribute("map", mapStr);
        
        return "index";
    }
    
    @RequestMapping("add_project")
    @ResponseBody
    public int AddProject(HttpServletRequest request) {
        String projectName = request.getParameter("project_name");
        String projectRemark = request.getParameter("project_remark");
        project.setProjectName(projectName);
        project.setProjectRemark(projectRemark);
        int effectLine = moduleService.addProject(project);
        return effectLine;
    }
    
    @RequestMapping("add_module")
    @ResponseBody
    public int AddModule(HttpServletRequest request) {
        String moduleName = request.getParameter("module_name");
        String moduleRemark = request.getParameter("module_remark");
        String tempid = request.getParameter("project_id");
        Integer project_id = Integer.valueOf(tempid);
        module.setModuleName(moduleName);
        module.setModuleRemark(moduleRemark);
        module.setProjectId(project_id);
        int effectLine = moduleService.addModule(module);
        return effectLine;
    }
    
}
